package com.java.flowable.myflowable.task;

import org.flowable.bpmn.model.*;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.lang.Process;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Demo {
    private final String USERTASKID="UserTaskId";

//    public BpmnModel createBpmnModel(CustomsFlowDefinition customsFlowDefinition) {
//        BpmnModel bpmnModel = new BpmnModel();
//        Process process = new Process();
//        process.setId(customsFlowDefinition.getFlowName());
//        createSequenceFlow(process,customsFlowDefinition);
//        createUserTask(process,customsFlowDefinition);
//        bpmnModel.addProcess(process);
//        return bpmnModel;
//    }
//
//    private Process createEvent(Process process, String flowName, LinkedList<SequenceFlow> linkedList) {
//        StartEvent startEvent = new StartEvent();
//        startEvent.setName("StartEventName");
//        startEvent.setId("StartEventId");
//        startEvent.setOutgoingFlows(Arrays.asList(linkedList.getFirst()));
//        process.addFlowElement(startEvent);
//        EndEvent endEvent = new EndEvent();
//        endEvent.setName( "EndEventName");
//        endEvent.setId("EndEventId");
//        endEvent.setOutgoingFlows(Arrays
//                .asList(linkedList.getLast()));
//        process.addFlowElement(endEvent);
//        return process;
//
//    }
//
//
//    private Process createSequenceFlow(Process process, CustomsFlowDefinition customsFlowDefinition) {
//        LinkedList<SequenceFlow> linkedList = new LinkedList<>();
//        for (int i = 0; i < customsFlowDefinition.getNodes().size() + 1; i++) {
//            SequenceFlow sequenceFlow = new SequenceFlow();
//            sequenceFlow.setId("SeqId" + i);
//            sequenceFlow.setName("SeqName" + i);
//            if (i == 0) {
//                sequenceFlow.setSourceRef("StartEventId");
//                sequenceFlow.setTargetRef(customsFlowDefinition.getNodes().get(0).getNodeKey());
//            }else if (i == customsFlowDefinition.getNodes().size()) {
//                sequenceFlow.setSourceRef(customsFlowDefinition.getNodes().get(customsFlowDefinition.getNodes().size()-1).getNodeKey());
//                sequenceFlow.setTargetRef("EndEventId");
//            }else {
//                sequenceFlow.setSourceRef(customsFlowDefinition.getNodes().get((i-1)).getNodeKey());
//                sequenceFlow.setTargetRef(customsFlowDefinition.getNodes().get((i)).getNodeKey());
//            }
//            sequenceFlow.setConditionExpression("${outcome=='agree'}");
//            process.addFlowElement(sequenceFlow);
//            linkedList.add(sequenceFlow);
//        }
//        createEvent(process,customsFlowDefinition.getFlowName(),linkedList);
//        return process;
//    }
//
//
//    private UserTask createUserTask(String taskId, String taskName,
//                                    SequenceFlow inSequenceFlow, SequenceFlow outSequenceFlow,
//                                    String userId) {
//        UserTask userTask = new UserTask();
//        userTask.setAssignee(userId);
//        userTask.setName(taskName);
//        userTask.setId(taskId);
//        return userTask;
//    }
//
//
//    private Process createUserTask(Process process, CustomsFlowDefinition customsFlowDefinition) {
//        for (int i = 0; i < customsFlowDefinition.getNodes().size(); i++) {
//            List<String> users = new ArrayList<String>();
//            users.add("${userIds}");
//            UserTask userTask = new UserTask();
//            String userTaskName=customsFlowDefinition.getNodes().get(i).getNodeName();
//            String userTaskId=customsFlowDefinition.getNodes().get(i).getNodeKey();
//            Assert.state(!StringUtils.isEmpty(userTaskId),"节点key不能为空");
//            userTask.setName(userTaskName==null?("UserTaskName"+i):userTaskName);
//            userTask.setId(userTaskId);
//            userTask.setCandidateUsers(users);
//            process.addFlowElement(userTask);
//        }
//        return process;
//    }

}
